<?php
/**
 * 短信发送
 */

namespace app\util;

use app\util\ReturnCode;
use app\util\Tools;
use think\Cache;
use think\Validate;
use Tencent\SMS as Service;

class SMS
{
    /*
     * @name 发送验证码
     * @param mobile  手机号
     */
    public function sendVerificationCode($mobile = '',$type='login'){

        $validate = new Validate([
            'mobile' => 'require|length:11'
        ],[
            'name.require' => '手机号码必须填写',
            'name.length' => '手机号码格式不正确'
        ]);

        $result  = $validate->check([
            'mobile' => $mobile
        ]);

        if (!$result){
            return Tools::buildReturn(ReturnCode::SMS_ERROR, $validate->getError());
        }

        $package = Cache::get($type . 'verificationCode' . $mobile);
        if (!empty($package)) {
            if ($package['outTime'] > THINK_START_TIME) {
                return Tools::buildReturn(ReturnCode::SMS_ERROR, '您发送验证码过快，请等待' . (int)($package['outTime'] - THINK_START_TIME) . '后重新发送');
            }
            if ($package['times'] > 100) {
                return Tools::buildReturn(ReturnCode::SMS_ERROR, '您发送的短信太频繁了，请稍后再次验证');
            }
        } else {
            $package = [
                'times'=>0
            ];
        }

        $code = mt_rand(1000, 9999);  //验证码

        $param = [
            $code
        ];

        $id = config('sms.codeID');
        if($type == 'setpaypassword'){
            $id = config('sms.payCodeID');
        }elseif($type == 'agentRegister'){
            $id = config('sms.AgentCodeID');
        }elseif($type == 'setAgentpassword'){
            $id = config('sms.AgentPasswordCodeID');
        }
        $res = Service::send_sma($mobile, $param, $id);

        if ($res['errmsg'] == 'OK') {
            $package['times']++;
            $package['outTime'] = THINK_START_TIME + 60;
            $package['code'] = $code;
            $package['count'] = 0;

            Cache::set($type.'verificationCode' . $mobile, $package, config('sms.EXPIRATION_TIME'));
            return Tools::buildReturn(ReturnCode::SUCCESS, '操作成功');
        }

        return Tools::buildReturn(ReturnCode::SMS_ERROR, $res['errmsg']);
    }

    //获取验证码
    public function checkVerificationCode($mobile = '', $code = '',$type='login') {
        $result = Cache::get($type.'verificationCode' . $mobile);
        if (!$result) {
            return Tools::buildReturn(ReturnCode::SMS_ERROR, '验证码已经失效了');
        }
        if ($result['count'] > 2) {
            Cache::set($type . 'verificationCode' . $mobile, NULL);
            return Tools::buildReturn(ReturnCode::SMS_ERROR, '错误超过3次，请重新发送验证码');
        }
        if ($result['code'] != $code) {
            $result['count']++;
            Cache::set($type.'verificationCode' . $mobile, $result, config('sms.EXPIRATION_TIME'));
            return Tools::buildReturn(ReturnCode::SMS_ERROR, '验证码错误');
        }
        Cache::set($type . 'verificationCode' . $mobile, NULL);
        return Tools::buildReturn(ReturnCode::SUCCESS, '操作成功');
    }

    /*
     * @name 群发验证码
     * @param mobilearray  手机号
     */
    public function sendVerificationList($mobile = [],$type = 0,$param =1){

        if(!is_array($mobile) || empty($mobile)){
            return Tools::buildReturn(ReturnCode::SMS_ERROR, "手机号错误");
        }
        $mobilestr = implode(',',$mobile);
        $package = Cache::get($type . 'verificationCode' . $mobilestr);
        if (!empty($package)) {
            if ($package['outTime'] > THINK_START_TIME) {
                return Tools::buildReturn(ReturnCode::SMS_ERROR, '您发送验证码过快，请等待' . (int)($package['outTime'] - THINK_START_TIME) . '后重新发送');
            }
            if ($package['times'] > 100) {
                return Tools::buildReturn(ReturnCode::SMS_ERROR, '您发送的短信太频繁了，请稍后再次验证');
            }
        } else {
            $package = [
                'times'=>0
            ];
        }

        $code = $type == 0 ? '下载App' :'佣金总计';  //验证码
        if($param == 1){
            $param = [
                $code
            ];
        }

        $id = config('sms.codeID');
        if($type == 0){
            $id = config('sms.payCodeID');
        }elseif ($type == 2){
            $id = config('sms.ReachOrderCodeID');
        }
        $res = Service::send_sma_array($mobile, $param, $id);

        if ($res['errmsg'] == 'OK') {
            $package['times']++;
            $package['outTime'] = THINK_START_TIME + 60;
            $package['code'] = $code;

            Cache::set($type.'verificationCode' . $mobilestr, $package, config('sms.EXPIRATION_TIME'));
            return Tools::buildReturn(ReturnCode::SUCCESS, '操作成功');
        }

        return Tools::buildReturn(ReturnCode::SMS_ERROR, $res['errmsg']);
    }

    /*
     * @name 发送提醒短信
     * @param mobile  手机号
     */
    public function sendMobileCode($mobile = '',$id,$params){

        $validate = new Validate([
            'mobile' => 'require|length:11'
        ],[
            'name.require' => '手机号码必须填写',
            'name.length' => '手机号码格式不正确'
        ]);

        $result  = $validate->check([
            'mobile' => $mobile
        ]);

        if (!$result){
            return Tools::buildReturn(ReturnCode::SMS_ERROR, $validate->getError());
        }

        $package = Cache::get($id . 'verificationCode' . $mobile);
        if (!empty($package)) {
            if ($package['outTime'] > THINK_START_TIME) {
                return Tools::buildReturn(ReturnCode::SMS_ERROR, '您发送短信过快，请等待' . (int)($package['outTime'] - THINK_START_TIME) . '后重新发送');
            }
            if ($package['times'] > 100) {
                return Tools::buildReturn(ReturnCode::SMS_ERROR, '您发送的短信太频繁了，请稍后再次验证');
            }
        } else {
            $package = [
                'times'=>0
            ];
        }

        $class = new \app\common\service\Sms();
        $res = $class->sendCode(['mobile' => $mobile]);

        if ($res['errcode'] == 'OK') {
            $package['times']++;
            $package['outTime'] = THINK_START_TIME + 60;

            Cache::set($id.'verificationCode' . $mobile, $package, config('sms.EXPIRATION_TIME'));
            return Tools::buildReturn(ReturnCode::SUCCESS, '操作成功');
        }

        return Tools::buildReturn(ReturnCode::SMS_ERROR, $res['errmsg']);
    }
}
